/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controle;

import basicas.Validador;
import dados.ConexaoDB;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author Junior
 */
public abstract class Controle {
  protected Validador val;
   
   public void cadastrar(Object o)throws IllegalArgumentException{
	if(!val.ehValido(o))throw new IllegalArgumentException(val.getError());
       Session s = ConexaoDB.getSessionFactory().getCurrentSession();
	s.save(o);
        
}
   public Object buscar(Class c,Serializable id){
	
        Session s = ConexaoDB.getSessionFactory().getCurrentSession();
	return s.load(c, id);
	
}
 
   public void remover(Object o)throws IllegalArgumentException{
	if(!val.ehValido(o))throw new IllegalArgumentException(val.getError());
        Session s = ConexaoDB.getSessionFactory().getCurrentSession();
	s.delete(o);
	
}
   
   public List listarProduto(String tbl){
	Session s = ConexaoDB.getSessionFactory().getCurrentSession();
	return s.createQuery(" from "+tbl).list();
	
}

      
    public Validador getVal() {
        return val;
    }

    public void setVal(Validador val) {
        this.val = val;
    }

   
   
   
}
